﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;


namespace Business
{
    public class DicionaryEnumBase : ObservableCollection<DicionaryItem>
    {
        public DicionaryEnumBase()
        {
        }

        private Type EnumType { get; set; }

        public void Load(Type enumType)
        {
            this.EnumType = enumType;
            Array enumValues = System.Enum.GetValues(enumType);
            foreach(Object t in enumValues){
                
                //AddChild(new ClienteWPF.DicionaryConverter.ItemMenu(t, GetDescription(t, enumType)));
                Add(new DicionaryItem
                {
                    Descricao = GetDescription(t, enumType) ,
                    Valor = t
                });
                
            }
        }
        
        private string GetDescription(object enumValue, Type enumType)
        {
            var descriptionAttribute = enumType
              .GetField(enumValue.ToString())
              .GetCustomAttributes(typeof(DescriptionAttribute), false)
              .FirstOrDefault() as DescriptionAttribute;


            return descriptionAttribute != null
              ? descriptionAttribute.Description
              : enumValue.ToString();
        }

        public Collection<DicionaryItem> GetLista()
        {
            return this;
        }
    }
}
